<?php
/**
 * Created by PhpStorm.
 * User: 86151
 * Date: 2024/3/6
 * Time: 11:29
 */

namespace app\api\controller;
use app\admin\command\Api;
use app\admin\model\Ctripinfo;
use app\admin\model\Orangs;
use app\admin\model\Wxpay;
use think\Config;
use think\Db;
use app\admin\model\Lianlians;
use app\admin\model\Auth;
use app\admin\model\Acxq as Acxqs;
use app\admin\model\Ctrip;
class WechatNotify extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    //爱吃回调
    public function acpay(){
        $xml = file_get_contents("php://input");
        $arr = $this -> XmlToArr($xml);
        $out_trade_no    = $arr['out_trade_no'];
        $transaction_id= $arr['transaction_id'];
        $order = Db::name("order")->where(['order_no'=>$out_trade_no, 'status'=>1])->find();
        if ($order) {
            $res = Acxqs::createOrder($out_trade_no);
            if ($res['code'] == 0) {
                Db::name("order")->where(['order_no' => $out_trade_no])->update(['status' => 2,'transaction_id'=>$transaction_id]);
                Db::name('goods')->where('item_id', $order['resource_id'])->setInc('total_sales');
                Db::name('goods')->where('item_id', $order['resource_id'])->setDec('stock');
                if ($order['qing_id'] > 0) {
                    Db::name('qinghao')->where('id', $order['qing_id'])->update(['is_do' => 1]);
                }
            } else {
                Db::name("order")->where([
                    'order_no' => $out_trade_no
                ])->update([
                    'status' => 5
                ]);
                Db::name("order")->where(['order_no' => $out_trade_no])->setField("desc", "购买失败,请退款给该客户。");
                $reason = '退款';
                Wxpay::doRefund($order['amount'],$order['amount'],$order['order_no'],$order['transaction_id'],$reason);
            }
        }
        echo 'success';
    }

    //橙券卡密
    public function cqkmpay(){
        $xml = file_get_contents("php://input");
        $arr = $this -> XmlToArr($xml);
        $out_trade_no    = $arr['out_trade_no'];
        $transaction_id= $arr['transaction_id'];
        //判断是否对订单处理过
        //判断是否对订单处理过
        $order = Db::name("order")->where([
            'order_no'=>$out_trade_no,
            'status'=>1
        ])->find();
        if ($order) {
            $res =  Orangs::ksend($order['num'],$order['resource_id']);
            if($res['code'] == 7000){
                foreach ($res['data'] as &$v){
                    Auth::kmjx($order, $v);
                }
                //完成支付
                Db::name("order")->where(['order_no' => $out_trade_no])->update([
                    'status' => 4,
                    'biz_order_id'=>$out_trade_no,
                    'transaction_id'=>$transaction_id
                ]);
                Db::name('goods')->where('item_id',$order['resource_id'])->setInc('total_sales');
                Db::name('goods')->where('item_id',$order['resource_id'])->setDec('stock');
                if($order['qing_id']>0){
                    Db::name('qinghao')->where('id', $order['qing_id'])->update(['is_do' => 1]);
                }
            }else{
                Db::name("order")->where([
                    'order_no' => $out_trade_no
                ])->update([
                    'status' => 5
                ]);
                Db::name("order")->where(['order_no'=>$out_trade_no])->setField("desc","购买失败,请退款给该客户。");
                $reason = '退款';
                Wxpay::doRefund($order['amount'],$order['amount'],$order['order_no'],$order['transaction_id'],$reason);

            }
        }
        echo 'success';
    }

    //橙券直充
    public function cqzcpay(){
        $xml = file_get_contents("php://input");
        $arr = $this -> XmlToArr($xml);
        $out_trade_no    = $arr['out_trade_no'];
        $transaction_id= $arr['transaction_id'];
        //判断是否对订单处理过
        $order = Db::name("order")->where([
            'order_no'=>$out_trade_no,
            'status'=>1
        ])->find();
        if ($order) {
            $res =  Orangs::send($order['mobile'],$order['order_no'],$order['resource_id']);
            if($res['code'] == 7000){
                //完成支付
                Db::name("order")->where(['order_no' => $out_trade_no])->update([
                    'status' => 2,
                    'biz_order_id'=>$out_trade_no,
                    'transaction_id'=>$transaction_id
                ]);
                Db::name('goods')->where('item_id',$order['resource_id'])->setInc('total_sales');
                Db::name('goods')->where('item_id',$order['resource_id'])->setDec('stock');
                if($order['qing_id']>0){
                    Db::name('qinghao')->where('id', $order['qing_id'])->update(['is_do' => 1]);
                }
            }else{
                Db::name("order")->where(['order_no' => $out_trade_no])->update(['status' => 5]);
                Db::name("order")->where(['order_no'=>$out_trade_no])->setField("desc","购买失败,请退款给该客户。");
                $reason = '退款';
                Wxpay::doRefund($order['amount'],$order['amount'],$order['order_no'],$order['transaction_id'],$reason);

            }
        }
        echo 'success';
    }

    //联联
    public function llpay(){
        $xml = file_get_contents("php://input");
        $arr = $this -> XmlToArr($xml);
        if ($arr['return_code'] == 'SUCCESS' && $arr['result_code'] == 'SUCCESS') {
            $out_trade_no    = $arr['out_trade_no'];
            $transaction_id= $arr['transaction_id'];
            //判断是否对订单处理过
            $order = Db::name("order")->where([
                'order_no'=>$out_trade_no,
                'transaction_id'=>$transaction_id,
                'status'=>1
            ])->find();
            if ($order) {
                $goods = Db::name('ll_itemlist')->where('itemId',$order['llgg_id'])->find();
                $lianlian = new Lianlians;
                $res =  $lianlian->creates($order['order_no'],$order['outer_order_id'],$order['resource_id'],$order['llgg_id'],$goods['channelPrice'],$order['llname'],$order['user_mobile'],$order['num'],$order['llthirdSalePrice'],$order['idcard'],$order['time']);
                if($res['code'] == 200){
                    $orderList = json_encode($res['data']['encryptedData']['orderList']);
                    Db::name("order")->where(['order_no' => $out_trade_no])->update([
                        'status' => 2,
                        'orderList'=>$orderList
                    ]);
                    Db::name('goods')->where('item_id',$order['resource_id'])->setInc('total_sales');
                    Db::name('goods')->where('item_id',$order['resource_id'])->setDec('stock');
                    if($order['qing_id']>0){
                        Db::name('qinghao')->where('id', $order['qing_id'])->update(['is_do' => 1]);
                    }
                }else{
                    Db::name("order")->where([
                        'order_no' => $out_trade_no
                    ])->update([
                        'status' => 5
                    ]);
                    Db::name("order")->where(['order_no'=>$out_trade_no])->setField("desc","购买失败,请退款给该客户。");
                    $reason = '退款';
                    Wxpay::doRefund($order['amount'],$order['amount'],$order['order_no'],$order['transaction_id'],$reason);
                }
            }
        }
        echo 'success';
    }
    //携程
    public function xcpay(){
        $xml = file_get_contents("php://input");
        $arr = $this -> XmlToArr($xml);
        $out_trade_no    = $arr['out_trade_no'];
        $transaction_id= $arr['transaction_id'];
        //判断是否对订单处理过
        $order = Db::name("order")->where([
            'order_no'=>$out_trade_no,
            'status'=>1
        ])->find();
        if ($order) {
            //完成支付
            $res = Ctripinfo::Ordercreate($out_trade_no,$order['scenicspotid'],$order['resource_id'],$order['time'],$order['num'],$order['price'],$order['cx_num'],$order['contactinfo'],$order['passenerinfolist'],$order['itemaddinfos']);
            if($res['ResultStatus']['IsSuccess'] === true){
                Db::name('order')->where([
                    'order_no' => $out_trade_no
                ])->update([
                    'outer_order_id' =>$res['OrderID'],
                    'biz_order_id' =>$res['TradeNo'],
                    'transaction_id'=>$transaction_id,
                    'status'=>2,
                ]);
                //添加销量
                Db::name('ctrip')->where('scenicspotid',$order['scenicspotid'])->setInc('ctrip_sales',$order['num']);
                Db::name('ctrip_resources')->where('resouceid',$order['resource_id'])->setInc('sales',$order['num']);
                $this->coupz($order['id']);
                if($order['qing_id']>0){
                    Db::name('qinghao')->where('id', $order['qing_id'])->update(['is_do' => 1]);
                }
            }else{
                Db::name("order")->where([
                    'order_no' => $out_trade_no
                ])->update([
                    'status' => 5
                ]);
                Db::name("order")->where(['order_no'=>$out_trade_no])->setField("desc","购买失败,请退款给该客户。");
                $reason = '退款';
                Wxpay::doRefund($order['amount'],$order['amount'],$order['order_no'],$order['transaction_id'],$reason);
            }
        }
        echo 'success';
    }
    public function voucher($xcid){
        $res = Ctrip::send('huoqumenpiaopinzheng',[
            'OrderId'=>$xcid,
            'SID'=>Config::get("ctrip.sid"),
            'VersionNO'=>'1.0',
            'AllianceID'=>Config::get("ctrip.aid")
        ]);
        return $res;
    }
    /**
     * 生成凭证接口
     */
    public function coupz($order_id){
        $order =  Db::name('order')->where('id',$order_id)->find();
        $voucher = $this->voucher($order['outer_order_id']);
        if($voucher['ResultStatus']['IsSuccess'] === true){
            if(isset($voucher['VoucherOrderItemInfoList'][0]['VoucherWrapInfoList'][0]['VoucherNO'])){
                $ticket_code = $voucher['VoucherOrderItemInfoList'][0]['VoucherWrapInfoList'][0]['VoucherNO'];
            }else{
                $ticket_code = $order['order_no'];
            }
            if(isset($voucher['VoucherOrderItemInfoList'][0]['ProductUseMsg'])){
                $ProductUseMsg = $voucher['VoucherOrderItemInfoList'][0]['ProductUseMsg'];
            }else{
                $ProductUseMsg = '';
            }
            if(isset($voucher['VoucherOrderItemInfoList'][0]['VendorVoucher'])){
                $VendorVoucher = $voucher['VoucherOrderItemInfoList'][0]['VendorVoucher'];
            }else{
                $VendorVoucher = '';
            }
            if(isset($voucher['VoucherOrderItemInfoList'][0]['ResourceName'])){
                $ResourceName = $voucher['VoucherOrderItemInfoList'][0]['ResourceName'];
            }else{
                $ResourceName = '';
            }

            Db::name('voucher')->insert([
                'item_id'=>makeOrderNum(),
                'voucher_id'=>$order['resource_id'],
                'effect_time'=>$order['createtime'],
                'expire_time'=>$order['time'],
                'title'=>$ResourceName,
                'ProductUseMsg'=>$ProductUseMsg,
                'VendorVoucher'=>$VendorVoucher,
                'ticket_code'=>$ticket_code,
                'type'=>3,
                'user_id'=>$order['user_id'],
                'order_id'=>$order['id'],
                'order_amount'=>$order['amount'],
            ]);
        }else{
            Db::name('voucher')->insert([
                'item_id'=>makeOrderNum(),
                'voucher_id'=>$order['resource_id'],
                'effect_time'=>$order['createtime'],
                'expire_time'=>$order['time'],
                'type'=>3,
                'user_id'=>$order['user_id'],
                'order_id'=>$order['id'],
                'order_amount'=>$order['amount'],
            ]);
        }
//        $this->success('生成凭证成功');
    }

    public function XmlToArr($xml){
        if($xml == '') return '';
        libxml_disable_entity_loader(true);
        $arr = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        return $arr;
    }
}